package com.uvt.agenda.activities;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.*;
import com.uvt.agenda.*;
import com.uvt.agenda.commands.CommandsManager;
import com.uvt.agenda.commands.GeneratePDFDocument;
import com.uvt.agenda.pdf.Builder;
import com.uvt.agenda.view.CalendarView;
import com.uvt.agenda.view.NewEventView;
import com.uvt.agenda.view.UndoBarController;
import com.uvt.agenda.commands.GeneratePDFDocument;
import com.uvt.agenda.pdf.PdfBuilder;
import com.uvt.agenda.pdf.PdfCalendar;
import android.content.Context;

import java.util.ArrayList;

/**
 * Created by Cristian Holdunu on 09/01/14.
 * 21:22
 */
public class CalendarActivity extends BaseActivity implements UndoBarController.UndoListener{


    private Spinner viewTypeSelector;
    private ImageButton nextMonth;
    private CalendarView calendarView;
    private ListView dayEventsList;
    private EventsListAdapter eventsListAdapter;
    private ArrayList<Event> dayEvents = new ArrayList<Event>();
    private UndoBarController undoBarController;

    public static GeneratePDFDocument generateMonthPdf;
    public static GeneratePDFDocument generateDayPdf;
    public static Context context;


    @Override
    public void onCreate(Bundle savedBundle){
        super.onCreate(savedBundle);
        context = this;

       //set layout
        setContentView(R.layout.calendar_activity);

        nextMonth = (ImageButton) findViewById(R.id.nextMonth);
        calendarView = (CalendarView) findViewById(R.id.month_view);
        viewTypeSelector = (Spinner) findViewById(R.id.viewTypeSelector);
        dayEventsList = (ListView) findViewById(R.id.dayEventsList);

        calendarView.setEventsProvider(EventsProvider.getInstance());
        nextMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendarView.scrollNextMonth();
            }
        });

        ArrayList<String> viewModes = new ArrayList<String>();
        viewModes.add("Month");
        viewModes.add("Week");
        viewModes.add("Day");

        ViewSpinnerAdapter viewSpinnerAdapter = new ViewSpinnerAdapter(this, viewModes);

        this.viewTypeSelector.setAdapter(viewSpinnerAdapter);
        this.viewTypeSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        eventsListAdapter = new EventsListAdapter(CalendarActivity.this, dayEvents);
        dayEventsList.setAdapter(eventsListAdapter);

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

                /**
                 *   There is a bug here. month is reported with one unit lower that in reality
                 *   For example: February = 1 instead of 2
                 */
                eventsListAdapter.clear();
                eventsListAdapter.addAll(EventsProvider.getInstance().getEventsFor(year, month+1, dayOfMonth).asList());
                CalendarActivity.generateMonthPdf = new GeneratePDFDocument(CalendarActivity.context,year,month+1);
                CalendarActivity.generateDayPdf = new GeneratePDFDocument(CalendarActivity.context,year,month+1,dayOfMonth);
                eventsListAdapter.notifyDataSetChanged();
            }
        });

        findViewById(R.id.export_pdf_month).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    CommandsManager.executeCommand(CalendarActivity.generateMonthPdf);

            }
        });

        findViewById(R.id.export_pdf_day).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    CommandsManager.executeCommand(CalendarActivity.generateDayPdf);

            }
        });

        calendarView.setShowWeekNumber(false);

        dayEventsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Event currentEvent = (Event)dayEventsList.getItemAtPosition(position);
                NewEventView evView = new NewEventView(CalendarActivity.this, currentEvent);
                evView.toggleViewMode();
                evView.show();
//                undoBarController.showUndoBar(true, "What should we do, Frank?", null);
            }
        });

        EventsProvider.getInstance().addObserver(calendarView);


        undoBarController = new UndoBarController(findViewById(R.id.undobar), this);
    }

    @Override
    public void onUndo(Parcelable token) {

        Toast.makeText(this, "test", Toast.LENGTH_LONG).show();
    }
}
